package com.gidgabs.modules.api.controller;

import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.modules.api.service.TbProductLikeService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.entity.TbProductLikeEntity;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 产品关系表-点赞表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-06-29 17:16:10
 */
@RestController
@RequestMapping("/api/productlike")
public class TbProductLikeController {

    @Autowired
    private TbProductLikeService productLikeService;

    @Autowired
    private TbUserService userService;

    /**
     * 添加点赞
     */
    @ApiOperation(value = "添加点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "被点赞产品id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/add")
    public ReturnJson<String> add(Integer productId) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        TbProductLikeEntity productLike = new TbProductLikeEntity();
        productLike.setUserIdMaster(user.getUserId());
        productLike.setRewardIdSlave(productId);
        productLikeService.save(productLike);
        return ReturnJson.success("操作成功");
    }

}
